package be.nepherte.movmanager.client.ui.components;

import be.nepherte.movmanager.client.MovManager;

import be.nepherte.movmanager.domain.Movie;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;


/**
 * This class represents a panel featuring a search box with button
 * 
 * <p>
 * Copyright © 2010 Bart Verhoeven
 * </p>
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * 
 * <p>
 * MovManger is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * 
 * <p>
 * MovManger is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * 
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManger. If not, see http://www.gnu.org/licenses/.
 * </p>
 * 
 * @author	Dieter Sauvillers
 * @version 0.1
 */

public class SearchPanel extends Composite implements ClickHandler{
	/**A panel holding the searchbox & button*/
	private final HorizontalPanel pnlSearch = new HorizontalPanel();
	/**A text field for a search string.*/
	private final TextBox txtSearch = new TextBox();
	/**A Search button.*/
	private final Button btnSearch = new Button(MovManager.constants.buttonSearch());
	

	
	public SearchPanel(){
		super();
		initSearchPanel();
	}
	
	/**
	 * Initializes the search panel.
	 */
	private void initSearchPanel(){
		formatSearchPanel();
		txtSearch.addClickHandler(this);
		pnlSearch.add(txtSearch);
		pnlSearch.add(btnSearch);
		initWidget(pnlSearch);
	}
	
	/**
	 * Formats the search panel.
	 */
	private void formatSearchPanel() {
		setToolTips();
		pnlSearch.setSpacing(10);
		pnlSearch.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		txtSearch.setText("Search ...");
		txtSearch.setSize("250px", "22px");
		btnSearch.setSize("80px", "22px");
	}
	
	/**
	 * Sets tool tips for widgets.
	 */
	private void setToolTips() {
		txtSearch.setTitle("Search for movie title");
		btnSearch.setTitle("Search");
	}

	@Override
	public void onClick(ClickEvent event) {
		// the search text box has been clicked
		if (event.getSource() == txtSearch)
			if (txtSearch.getText().equals("Search ..."))
				txtSearch.setText("");
		
	}
	
	/**
	 * Resets all active filters.
	 */
	public void reset() {
		txtSearch.setText("Search ...");
	}
	
	
	/**
	 * Search for movies based on title.
	 */
	public List<Movie> searchMovies(List<Movie> movies){
		List<Movie> matchedMovies = new ArrayList<Movie>();
		for(Movie m : movies){
			if(m.getTitle().indexOf(txtSearch.getText()) >= 0){
				matchedMovies.add(m);
			}
		}
		
		return matchedMovies;
	}
	

	/**
	 * Sets the action to be performed when the search button has been clicked.
	 * 
	 * @param	h
	 * 			click handler
	 */
	public void setFilterAction(ClickHandler h) {
		btnSearch.addClickHandler(h);
	}
}
